clear
[ mean_frame_name,mean_frame_path ]=uigetfile({'*.png'});%'H:\My Drive\Propant Flow\Manuscript\Figs\Fig 4 Rheomicroscope\05-29-21 Test 20 30%CA50 30SR LAOS-selected frame.tif';
mean_frame=imread([mean_frame_path,mean_frame_name]);

%% 

[ video_file_name,video_file_path ] = uigetfile({'*.avi'});

input_video_file = [video_file_path,video_file_name];
% Read Video
videoFReader = VideoReader(input_video_file);
%%
output_image_path = fullfile(video_file_path,[video_file_name,'normalized.avi']);
frames=read(videoFReader);
% mean_frame=mean(frames,4);
videoFWrite = VideoWriter(output_image_path,'Grayscale AVI');
videoFWrite.FrameRate=50;
open(videoFWrite);
for count = 1:size(frames,4)

    key_frame =double(frames(:,:,1,count))./double((10+mean_frame));
% double(frames(:,:,1,count))./(10+mean_frame);
%     figure()
%     imshow(key_frame/256)    
%     imshow(key_frame)    
%     key_frame =double(frames(:,:,1,count))./(mean_frame);
    key_frame=key_frame*0.8;
    key_frame(key_frame>=1)=1;
    writeVideo(videoFWrite,key_frame);
end

close(videoFWrite);
disp('COMPLETED');
